package com.guigu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.guigu.LbtService;
import com.guigu.QuoteService;

import com.guigu.pojo.Quote;
import com.guigu.vo.Lbt;
import com.guigu.vo.LbtVo;
import com.guigu.vo.Pojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@CrossOrigin
@RequestMapping("Quote")
@RestController
public class QuoteController {
    @Autowired
    private LbtService lbtService;
    @Autowired
    private QuoteService quoteService;
    //供应商报价查询所有商品
    @RequestMapping("selectAll/{pageNo}/{pageSize}")
    public Pojo<Lbt> selectAll( @PathVariable int pageNo, @PathVariable int pageSize,@RequestBody LbtVo vo) {
        PageInfo<Lbt> lbtPageInfo = lbtService.selectAll1(vo, pageNo, pageSize);
        Pojo<Lbt> pojo = new Pojo<>();
        pojo.setRows(lbtPageInfo.getList());
        pojo.setTotal(lbtPageInfo.getTotal());
        return pojo;
    }
    //新增
    @PostMapping("insert")
    public String insert(@RequestBody Quote quote) {
        quote.setIsSellStatus(0);
        boolean save = quoteService.save(quote);
        if (save) {
            return "ok";
        }
        return "no";
    }
    //查询（是否已报价过）
    @GetMapping("selectOne/{commodityId}/{userId}")
    public Quote selectOne(@PathVariable int commodityId,@PathVariable int userId) {
        QueryWrapper<Quote> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("commodity_id",commodityId);
        objectQueryWrapper.eq("user_id",userId);
        Quote quote = quoteService.getOne(objectQueryWrapper);
        if (!Objects.equals(null,quote)){
            return quote;
        }else
        {
            return null;
        }

    }
    //修改
    @PutMapping("updateById")
    public String updateById(@RequestBody Quote quote) {
        boolean b = quoteService.updateById(quote);
        if (b)
            return "ok";
        return "no";
    }
}
